package com.sky.controller.admin;


import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {

    @Resource
    private DishService dishService;

    /**
     * 菜品新增接口
     * @param dishDTO
     * @return
     */
    @ApiOperation("菜品新增接口")
    @PostMapping
    public Result add(@RequestBody DishDTO dishDTO){
        log.info("菜品新增接口:{}",dishDTO);

        //调用业务新增菜品
        dishService.add(dishDTO);

        //返回数据
        return Result.success();
    }


    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @ApiOperation("菜品分页查询接口")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询接口：{}",dishPageQueryDTO);

        //调用分页查询业务
        PageResult pageResult = dishService.page(dishPageQueryDTO);

        //返回数据
        return Result.success(pageResult);
    }

    /**
     *菜品删除
     * @return
     */
    @ApiOperation("菜品删除接口")
    @DeleteMapping
    public Result delete(@RequestParam List<Long> ids){
        log.info("删除菜品接口:{}",ids);

        //调用业务进行批量删除
        dishService.delete(ids);

        //返回数据
        return Result.success();
    }


    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @ApiOperation("根据id查询菜品接口")
    @GetMapping("/{id}")
     public Result<DishVO> findById(@PathVariable Long id){
        log.info("根据id查询回显接口：{}",id);

        //调用业务根据id查询
         DishVO dishVO = dishService.findById(id);

         //返回数据
         return Result.success(dishVO);
     }


    /**
     * 更新菜品
     * @param dishDTO
     * @return
     */
    @ApiOperation("更新菜品接口")
    @PutMapping
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("根据id更新菜品：{}",dishDTO);

        //调用业务修改菜品
        dishService.update(dishDTO);

        //返回数据
        return Result.success();
    }


    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(long categoryId){
        log.info("根据分类id查询菜品接口：{}",categoryId);
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }
}
